package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsJjf1zBo;
import com.ruoyi.tb.lmt.domain.vo.McgsJjf1zVo;
import com.ruoyi.tb.lmt.domain.McgsJjf1z;
import com.ruoyi.tb.lmt.mapper.McgsJjf1zMapper;
import com.ruoyi.tb.lmt.service.IMcgsJjf1zService;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_紧急阀1组_MCGSService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsJjf1zServiceImpl implements IMcgsJjf1zService {

    private final McgsJjf1zMapper baseMapper;

    /**
     * 查询制动阀试验台_紧急阀1组_MCGS
     */
    @Override
    public McgsJjf1zVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_紧急阀1组_MCGS列表
     */
    @Override
    public TableDataInfo<McgsJjf1zVo> queryPageList(McgsJjf1zBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsJjf1z> lqw = buildQueryWrapper(bo);
        // 在这里添加根据 createTime 降序排序
        pageQuery.setOrderByColumn("createTime");
        pageQuery.setIsAsc("desc");
        Page<McgsJjf1zVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_紧急阀1组_MCGS列表
     */
    @Override
    public List<McgsJjf1zVo> queryList(McgsJjf1zBo bo) {
        LambdaQueryWrapper<McgsJjf1z> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsJjf1z> buildQueryWrapper(McgsJjf1zBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsJjf1z> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsJjf1z::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsJjf1z::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTime()), McgsJjf1z::getMcgsTime, bo.getMcgsTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsCzz()), McgsJjf1z::getMcgsCzz, bo.getMcgsCzz());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeMs()), McgsJjf1z::getMcgsTimeMs, bo.getMcgsTimeMs());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbh()), McgsJjf1z::getMcgsJxbh, bo.getMcgsJxbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsFh()), McgsJjf1z::getMcgsFh, bo.getMcgsFh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsSch()), McgsJjf1z::getMcgsSch, bo.getMcgsSch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf1()), McgsJjf1z::getMcgsJjf1, bo.getMcgsJjf1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf2()), McgsJjf1z::getMcgsJjf2, bo.getMcgsJjf2());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf3()), McgsJjf1z::getMcgsJjf3, bo.getMcgsJjf3());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf4()), McgsJjf1z::getMcgsJjf4, bo.getMcgsJjf4());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf5()), McgsJjf1z::getMcgsJjf5, bo.getMcgsJjf5());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf6()), McgsJjf1z::getMcgsJjf6, bo.getMcgsJjf6());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf7()), McgsJjf1z::getMcgsJjf7, bo.getMcgsJjf7());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf8()), McgsJjf1z::getMcgsJjf8, bo.getMcgsJjf8());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf9()), McgsJjf1z::getMcgsJjf9, bo.getMcgsJjf9());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf10()), McgsJjf1z::getMcgsJjf10, bo.getMcgsJjf10());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsBy1()), McgsJjf1z::getMcgsBy1, bo.getMcgsBy1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZsch()), McgsJjf1z::getMcgsZsch, bo.getMcgsZsch());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsJjf1z::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsJjf1z::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsJjf1z::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsJjf1z::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsJjf1z::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }


    /**
     * 新增制动阀试验台_紧急阀1组_MCGS
     */
    @Override
    public Boolean insertByBo(McgsJjf1zBo bo) {
        McgsJjf1z add = BeanUtil.toBean(bo, McgsJjf1z.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_紧急阀1组_MCGS
     */
    @Override
    public Boolean updateByBo(McgsJjf1zBo bo) {
        McgsJjf1z update = BeanUtil.toBean(bo, McgsJjf1z.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsJjf1z entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_紧急阀1组_MCGS
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getMaxMcgsTime(){
        return baseMapper.getMaxMcgsTime();
    }

    @Override
    public List<McgsJjf1z> selectListByDate(LocalDate date) {
        return baseMapper.selectListByDate(date);
    }
}
